import tableHeader from "./__components/tableHeader";
import hmTable from "./__components/hmTable";
import { mapActions } from "vuex";
import mixins from "../mixins.js";
import { getHmentitymodelList } from "@/apis/paas/HmEntityModelController.js";
import { differenceWith, concat } from "lodash";
import { TABLE_RETAIN_CODE } from "@/utils/const";
import tableTitle from './__components/tableTitle'
import tableButtons from './__components/tableButtons'
export default {
  computed: {
    activeHeader() {
      return this.getModelForm.key || "" + this.getModelForm.code || "";
    },
  },
  watch: {
    "config.__config.modelCode": {
      handler: async function (val) {
        if (!!val) {
          this.config.__config.getSearchmodelList = [];
          this.config.__config.getEntitymodelList = [];
          this.gethmList(val);
        }
      },
    },
    "config.__config.independentAdvancedSearch"(val) {
      if (!val) {
        this.$nextTick(() => {
          this.handleAdd("tb_h");
        });
      }
    },
  },
  mixins: [mixins],
  components: { tableHeader, hmTable, tableTitle, tableButtons },
  async created() {
    if (!!this.config.hlcustomBtn) {
      this.config.hlcustomBtn.key = this.config.hlcustomBtn.tagName + this.config.key;
    }
    if (!!this.config.__config.modelCode) {
      this.gethmList(this.config.__config.modelCode, false);
    }
  },
  data() {
    return {
      total: 100,
      tableData: [{}, {}, {}, {}, {}],
      addDifferIds: [],
      deleteDifferIds: [],
    };
  },
  methods: {
    isAcitve(tagName) {
      return tagName + this.config.key === this.getModelForm.key;
    },
    addKeysForSearch(list, show) {
      return list.map(i => Object.assign({
        showSearch: show && i.createType !== "SYSTEM", // 是否显示
        title: i.name, // table 标题
        placeholder: "请输入",
        code: i.code,
        format: "YYYY-MM-DD", // 日期类型
        isRange: true, // 日期是否是区间
        isCurrentDate: false, // 是否默认显示当天
        matchRules: "FUZZY_MATCH", //模糊匹配 //匹配规则 'EXACT_MATCH' 精确匹配
        title_i18n: i.localism,
        placeholder_i18n: "",

      }, i))
    },
    addKeysForColumn(list, show) {
      return list.map(i => Object.assign({
        title: i.name, //table 标题
        title_i18n: i.localism,
        showSearch: true, //是否显示查询
        showColumn: show && i.createType !== "SYSTEM", //是否显示
        fixed: false, //是否冻结
        widthType: "1", //1 自适应 2固定
        width: 80, //宽度
        renderJsx: "", //自定义渲染jsx函数
        isRender: false, //是否自定义渲染
        format: "YYYY-MM-DD", //日期类型
        sort: false, //是否排序查询
        textEdit: false, //是否编辑文本
        textEditvalidate: '',//自定义表单校验
        textEditType: '',//编辑组件类型
        textEditRoot: 'serve',//服务端编辑，前端编辑 web serve
        textEditCustom: false, //是否开启自定义服务
        textEditCustomServe: '',//自定义服务函数
        showSummary: false, //是否合计
        total_unit: "", //合计单位
        showExplain: false,//表头说明
        explainValue: "",//表头说明
        explainValue_i18n: "",
      }, i))
    },
    async gethmList(modelKey, show = true) {
      let hmentitymodelList = await getHmentitymodelList({
        activated: true,
        modelKey,
      });
      //取消是否已经删除字段
      hmentitymodelList = hmentitymodelList.filter(
        (i) => i.code !== "deleted" && TABLE_RETAIN_CODE.indexOf(i.type) > -1
      );
      if (show) {
        this.config.__config.getSearchmodelList = this.addKeysForSearch(hmentitymodelList, show);
        this.config.__config.getEntitymodelList = this.addKeysForColumn(hmentitymodelList, show);
      } else {
        let { getSearchmodelList, getEntitymodelList } = this.config.__config;
        getEntitymodelList = getEntitymodelList.map(item => {
          return {
            ...item,
            showExplain: item.showExplain || false
          }
        })
        let searchlist = this.transformList(
          hmentitymodelList,
          getSearchmodelList,
          "showSearch"
        );
        let entitylist = this.transformList(
          hmentitymodelList,
          getEntitymodelList,
          "showColumn"
        );
        this.config.__config.getSearchmodelList = this.addKeysForSearch(searchlist, show)
        this.config.__config.getEntitymodelList = this.addKeysForColumn(entitylist, show)
      }
    },
    //集合差异对比，新增的字段插入已有的字段数据里
    transformList(list, modellist, key) {
      //新增的
      let addDifference = differenceWith(
        list,
        modellist,
        (x, y) => x.id === y.id
      );
      //删除的
      let deleteDifference = differenceWith(
        modellist,
        list,
        (x, y) => x.id === y.id
      );
      //多余的字段id
      this.addDifferIds = addDifference.map((i) => i.id);
      //删除的字段id
      this.deleteDifferIds = deleteDifference.map((i) => i.id);
      //删除的没被选中的直接过滤
      modellist = modellist.filter((i) => {
        return i[key] || this.deleteDifferIds.indexOf(~~i.id) === -1;
      });
      return concat(modellist, addDifference);
    },
    ...mapActions(["setModelForm"]),
    async handleAdd(tagName, value = {}) {
      const key = tagName + this.config.key;
      switch (tagName) {
        case "tb_h":
          this.setModelForm({
            tagName,
            key,
            name: "独立搜索",
            addDifferIds: this.addDifferIds,
            deleteDifferIds: this.deleteDifferIds,
            getSearchmodelList: this.config.__config.getSearchmodelList,
            __config: this.config.__config,
          });
          break;
        case "tb_h_i":
          this.setModelForm({});
          await this.$nextTick();
          this.setModelForm(
            !this.config.__config.independentAdvancedSearch
              ? Object.assign(value, { tagName, key })
              : value
          );
          break;
        case "tb_lt":
          this.setModelForm({
            tagName,
            key,
            name: "列表配置",
            addDifferIds: this.addDifferIds,
            deleteDifferIds: this.deleteDifferIds,
            getEntitymodelList: this.config.__config.getEntitymodelList,
          });
          break;
        case "tb_im":
          this.setModelForm(
            Object.assign(value, {
              tagName,
              key: key + value.code,
              searchStatus: this.config.__config.searchStatus,
              __showSummary__: this.config.__config.showSummary,
            })
          );
          break;
        case "tb_o":
          this.setModelForm(
            Object.assign(this.config.__config.operate, {
              tagName,
              key: key,
              name: "操作",
              moduleId: this.config.__config.moduleId,
              modelCode: this.config.__config.modelCode,
            })
          );
          break;
        case value.tagName:
          this.setModelForm(Object.assign(value, { key }));
          break;
        default:
          break;
      }
    },
  },
};